import { type IUserLogin } from "@/validators/User.valid";
import jwt from "jsonwebtoken";
import assert from "assert";
import env from "@/utils/env";

export const login = async (param: IUserLogin) => {
  const { username, password } = param || {};

  // 模拟验证用户名和密码，可替换其他代码，如数据库查询，密码加密等
  assert(
    username === "admin" && password === "123456",
    "用户名或密码错误:88888"
  );

  // 生成 token
  const token = jwt.sign({ username }, env.JWT_SECRET, { expiresIn: "1h" });

  // 模拟登陆成功后返回用户信息
  const user = {
    username: "admin",
    nickname: "超级管理员",
    role: "admin",
  };

  return { token, user };
};
